-module(device_manager).
-include("definitions.hrl").

-export([get_device_list/0, get_device_list/2]).

-spec get_device_list/0 :: () -> [device()].
get_device_list() ->
  {ok, Source} = application:get_env(device_source),
  X = case (Source) of

        mock -> Descriptors = mock_devices_file:read_device_list_from_file(),
          DescriptorsStarted = run_drivers(Descriptors, []),
          DescriptorsStarted;

        android ->
          android_device:connect()
      end,
  X.

-spec get_device_list/2 :: ([device_id()], [device()]) -> [device()].
get_device_list([], Acc) ->  run_drivers(Acc, []);

get_device_list([DevId | Rest], Acc) ->
  Device = #deviceDescriptor{id = DevId, description = "empty"},
  get_device_list(Rest, [Device| Acc]).


-spec run_drivers/2 :: ([device()], [device()]) -> [device()].
run_drivers([], Acc) -> Acc;
run_drivers([Descr | Rest], Acc) ->
  Pid = spawn(device_interface, start, [Descr]),
  NewDesc = Descr#deviceDescriptor{driver = Pid},
  run_drivers(Rest, [NewDesc | Acc]).

